﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace AIC_CS
{
    public partial class MainReport_View : MyUserBase
    {
        private string MySwitchCase;
        private static string FilePath = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            lblDonVi.Text = MyCommon.GetTenDonVi(Session["userid"].ToString());
            BindDataToDropDownList(ddlKyBaoCao, "KyBaoCao");

            MySwitchCase = Session["SwitchCase"].ToString();

            if (Session["SwitchCase"].ToString() != "Null")
            {
                BinDataToControl();

                if (Session["SwitchCase"].ToString() == "Detail")
                {
                    btnXemCiTiet.Enabled = false;
                    ddlKyBaoCao.Enabled = false;
                    txtbaocaoso.Enabled = false;
                    fulFileDinhKem.Visible = false;
                    txtDenNgay.Enabled = false;
                }
                else
                    if (Session["SwitchCase"].ToString() == "Sua")
                    {
                        btnXemCiTiet.Enabled = true;
                        ddlKyBaoCao.Enabled = true;
                        txtbaocaoso.Enabled = true;
                        fulFileDinhKem.Visible = true;
                        txtDenNgay.Enabled = true;
                    }
            }


        }

        protected void ddlKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnXemCiTiet_Click(object sender, EventArgs e)
        {

        }

        protected void lbtFileDinhkem_Click(object sender, EventArgs e)
        {
            if (FilePath.Trim().Length != 0)
            {
                string FileName = FilePath.Split('/')[2];
                MyCommon.Download(FileName, FilePath);
            }
        }

        protected void lbtFileDinhkem_created_Click(object sender, EventArgs e)
        {

        }
        protected void BinDataToControl()
        {
            string sql = "Select KyBaoCaoID_DM,BaoCaoSo,FileDinhKem,CONVERT(VARCHAR(10),UpdateDate,103) as UpdateDate, NoiDung From tblBaoCaoKetQuaCongTacChinhSachTrongTamCongTacChinhSach Where id='" + Session["ReportID"] + "' ";
            SqlDataReader myReader = SqlHelper.ExecuteReader(CommandType.Text, sql);
            myReader.Read();
            lblDonVi.Text = MyCommon.GetTenDonVi(Session["userid"].ToString());
            ddlKyBaoCao.Items.FindByValue(myReader["KyBaoCaoID_DM"].ToString()).Selected = true;
            txtbaocaoso.Text = myReader["BaoCaoSo"].ToString();
            lbtFileDinhkem.Text = myReader["FileDinhKem"].ToString();
            FilePath = myReader["FileDinhKem"].ToString();
            txtDenNgay.Text = myReader["UpdateDate"].ToString();
            txtNoiDung.Value = myReader["NoiDung"].ToString();
        }
        protected void BindDataToDropDownList(DropDownList BindDropDown, string MaDM)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@MaDM", SqlDbType.NVarChar, ParameterDirection.Input, MaDM));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblDanhMuc WHERE MaDM=@MaDM", arrParams);
            BindDropDown.DataSource = dt;
            BindDropDown.DataTextField = "TenDM";
            BindDropDown.DataValueField = "ID";
            BindDropDown.DataBind();
        }
    }
}